package com.feeyin.radar.crm.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.model.ContractCaseType;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.ProcessGroup;

public class CustomerDAOImpl extends SqlMapClientDaoSupport
  implements CustomerDAO
{
  public List getDeptSucceedContract(Customer customer)
  {
    return getSqlMapClientTemplate().queryForList("getDeptSucceedContract", customer);
  }
  public Integer getDeptSucceedContractCount(Customer customer) {
    return (Integer)getSqlMapClientTemplate().queryForObject("getDeptSucceedContractCount", customer);
  }
  public List getMyShareCustomer(Customer customer) {
    return getSqlMapClientTemplate().queryForList("deptNameCustomer", customer);
  }
  public Integer getMyShareCustomerCount(Customer customer) {
    return (Integer)getSqlMapClientTemplate().queryForObject("deptNameCustomerCount", customer);
  }
  public List getOwnNameCustomer(Customer customer) {
    return getSqlMapClientTemplate().queryForList("deptNameCustomer", customer);
  }
  public Integer getOwnNameCustomerCount(Customer customer) {
    return (Integer)getSqlMapClientTemplate().queryForObject("deptNameCustomerCount", customer);
  }
  public Integer getDeptNameCustomerCount(Customer customer) {
    return (Integer)getSqlMapClientTemplate().queryForObject("deptNameCustomerCount", customer);
  }

  public List getDeptNameCustomer(Customer customer) {
    return getSqlMapClientTemplate().queryForList("deptNameCustomer", customer);
  }

  public List getEveryMonthNewCustomerCount(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getEveryMonthNewCustomerCount", customer);
  }

  public List getTotalYearNewCustomerCount(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getTotalYearNewCustomerCount", customer);
  }

  public List getCustomerCountByAreaByAll(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getCustomerCountByAreaByAll", customer);
  }

  public List getCustomerCountByCompanyTypeByAll(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getCustomerCountByCompanyTypeByAll", customer);
  }

  public List getCustomerCountByIndustryByAll(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getCustomerCountByIndustryByAll", customer);
  }

  public Integer getCountAllCustomerInfoForContract(Customer customer) {
    return (Integer)getSqlMapClientTemplate().queryForObject("getCountAllCustomerInfoForContract", customer);
  }

  public Integer getCountOldCustomerContractInfo(Customer customer) {
    return (Integer)getSqlMapClientTemplate().queryForObject("getCountOldCustomerContractInfo", customer);
  }

  public List getAllCustomerInfoForContract(Customer customer) {
    List cs = getSqlMapClientTemplate().queryForList("getAllCustomerInfoForContract", customer);
    List cs1 = new ArrayList();
    for (int i = 0; i < cs.size(); i++) {
      Customer ctomer = (Customer)cs.get(i);
      if (ctomer.getTotalcontractCost() != null) {
        ctomer.setAvgContractCost(ctomer.getTotalcontractCost().divide(new BigDecimal(ctomer.getContractCount().intValue()), 2, 5));
      }

      cs1.add(ctomer);
    }
    return cs;
  }

  public List getCustomerCountByArea(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getCustomerCountByArea", customer);
  }

  public List getCustomerCountByCompanyType(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getCustomerCountByCompanyType", customer);
  }

  public List getCustomerCountByIndustry(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getCustomerCountByIndustry", customer);
  }

  public List getOldCustomerContractInfo(Customer customer)
  {
    List cs = getSqlMapClientTemplate().queryForList("getOldCustomerContractInfo", customer);
    List cs1 = new ArrayList();
    for (int i = 0; i < cs.size(); i++) {
      Customer ctomer = (Customer)cs.get(i);
      ctomer.setAvgContractCost(ctomer.getTotalcontractCost().divide(new BigDecimal(ctomer.getContractCount().intValue()), 2, 5));
      cs1.add(ctomer);
    }
    return cs1;
  }

  public List getCustomers() {
    return getSqlMapClientTemplate().queryForList("getCustomers");
  }

  public List getCustomersByDynamic(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getCustomersByDynamic", customer);
  }

  public List getRepeatCustomersByDynamic(Customer customer) {
    return getSqlMapClientTemplate().queryForList("getRepeatCustomersByDynamic", customer);
  }

  public List getPageCustomersByDynamic(Customer customer, Page page) {
    if (customer == null) return null;
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    return getSqlMapClientTemplate().queryForList("getPageCustomersByDynamic", customer);
  }

  public Integer getPageCustomersCountByDynamic(Customer customer)
  {
    return (Integer)getSqlMapClientTemplate().queryForObject("getPageCustomersCountByDynamic", customer);
  }

  public List getPageRepeatCustomersByDynamic(Customer customer, Page page) {
    if (customer == null) return null;
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    return getSqlMapClientTemplate().queryForList("getPageRepeatCustomersByDynamic", customer);
  }

  public Integer getPageRepeatCustomersCountByDynamic(Customer customer) {
    return (Integer)getSqlMapClientTemplate().queryForObject("getPageRepeatCustomersCountByDynamic", customer);
  }

  public List getCustomersByDeptId(Integer deptId) {
    if (deptId == null) return null;
    Customer customer = new Customer();
    customer.setDeptId(deptId);
    return getSqlMapClientTemplate().queryForList("getCustomersByDynamic", customer);
  }

  public Customer getCustomerById(Integer id) {
    if (id == null) return null;
    return (Customer)getSqlMapClientTemplate().queryForObject("getCustomerById", id);
  }

  public Customer getCustomerByCustomerCode(String customerCode) {
    if (StringUtils.isEmpty(customerCode)) return null;
    return (Customer)getSqlMapClientTemplate().queryForObject("getCustomerByCustomerCode", customerCode);
  }

  public Customer getCustomerSaleInfoById(Integer id) {
    if (id == null) return null;
    return (Customer)getSqlMapClientTemplate().queryForObject("getCustomerSaleInfoById", id);
  }

  public Integer getCustomersCountByDynamic(Customer customer) {
    return (Integer)getSqlMapClientTemplate().queryForObject("getCustomersCountByDynamic", customer);
  }

  public BigDecimal getTotalPredictCostByDynamic(Customer customer) {
    if (customer == null) return new BigDecimal(0);
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getTotalPredictCostByDynamic", customer);
  }

  public BigDecimal getPeriodPredictCost(Integer userId, String assignPeriod) {
    if (userId == null) return new BigDecimal(0);
    if (StringUtils.isEmpty(assignPeriod)) return new BigDecimal(0);

    Date startDate = DateUtils.getCurrentYearFirstDay();
    Date endDate = DateUtils.getCurrentYearFirstDay();

    if ("1".equals(assignPeriod)) {
      startDate = DateUtils.getCurrentMonthFirstDay();
      endDate = DateUtils.getCurrentMonthEndDay();
    } else if ("2".equals(assignPeriod)) {
      startDate = DateUtils.getCurrentSeasonFirstDay();
      endDate = DateUtils.getCurrentSeasonEndDay();
    } else if ("3".equals(assignPeriod)) {
      startDate = DateUtils.getCurrentYearFirstDay();
      endDate = DateUtils.getCurrentYearFirstDay();
    }

    Customer customer = new Customer();
    customer.setSalePersonId(userId);
    customer.setIsSaleStage("1");
    customer.setStartDate(startDate);
    customer.setEndDate(endDate);
    customer.setCategorys("1,3");
    return getTotalPredictCostByDynamic(customer);
  }

  public boolean insertCustomer(Customer customer) {
    if (customer == null) return false;
    if (StringUtils.isEmpty(customer.getCustomerCode())) return false;
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertCustomer", customer);

    return getCustomerByCustomerCode(customer.getCustomerCode()) != null;
  }

  public boolean updateCustomer(Customer customer)
  {
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateCustomer", customer));
    return result != null;
  }

  public boolean updateCustomerByParam(Customer customer)
  {
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateCustomerByParam", customer));
    return result != null;
  }

  public boolean updateCustomerByStaticParam(Customer customer)
  {
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateCustomerByStaticParam", customer));
    return result != null;
  }

  public boolean updateCustomerShare(Integer customerId, String isShare)
  {
    if (customerId == null) return false;
    if (StringUtils.isEmpty(isShare)) return false;
    Customer customer = new Customer();
    customer.setId(customerId);
    customer.setIsShare(isShare);
    return updateCustomer(customer);
  }

  public boolean deleteCustomer(Integer id) {
    Integer result = Integer.valueOf(getSqlMapClientTemplate().delete("deleteCustomer", id));
    return result != null;
  }
  
	public Integer getPageCaseTypeCountByDynamic(ContractCaseType caseType) {
		return (Integer)getSqlMapClientTemplate().queryForObject("getPageCaseTypeCountByDynamic", caseType);
	}
	
	public Integer getPageProcessGroupCountByDynamic(ProcessGroup processGroup) {
		return (Integer)getSqlMapClientTemplate().queryForObject("getPageProcessGroupCountByDynamic", processGroup);
	}
	
	public List<ProcessGroup> getPageProcessGroupByDynamic(ProcessGroup processGroup,Page page) {
		if(processGroup == null)return null;
		return getSqlMapClientTemplate().queryForList("getPageProcessGroupByDynamic",processGroup);
	}
	
	public List<ContractCaseType> getPageCaseTypesByDynamic(ContractCaseType caseType,Page page) {
		if(caseType == null)return null;
		return getSqlMapClientTemplate().queryForList("getPageCaseTypeByDynamic",caseType);
	}		
}